package com.platform.web.controller.system;

import java.util.List;
import java.util.Map;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.platform.common.annotation.Log;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.enums.BusinessType;
import com.platform.system.domain.ProCart;
import com.platform.system.service.IProCartService;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;

/**
 * 购物车Controller
 *
 * @author platform
 * @date 2025-02-23
 */
@Tag(name = "购物车", description = "")
@RestController
@RequestMapping("/system/cart")
public class ProCartController extends BaseController {
    @Autowired
    private IProCartService proCartService;

    /**
     * 查询购物车列表
     */
    @Operation(summary = "查询购物车列表")
    @PreAuthorize("@ss.hasPermi('system:cart:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProCart proCart) {
        startPage();
        List<ProCart> list = proCartService.selectProCartList(proCart);
        return getDataTable(list);
    }

    /**
     * 查询购物车列表
     */
    @Operation(summary = "查询购物车列表")
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        ProCart proCart = new ProCart();
        proCart.setUserId(getUserId());
        List<ProCart> carts = proCartService.selectProCartListAll(proCart);
        Map<String, Object> all = proCartService.findAll(carts);
        return success(all);
    }

    /**
     * 导出购物车列表
     */
    @Operation(summary = "导出购物车列表")
    @PreAuthorize("@ss.hasPermi('system:cart:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProCart proCart) {
        List<ProCart> list = proCartService.selectProCartList(proCart);
        ExcelUtil<ProCart> util = new ExcelUtil<ProCart>(ProCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @Operation(summary = "获取购物车详细信息")
    @PreAuthorize("@ss.hasPermi('system:cart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(proCartService.selectProCartById(id));
    }

    /**
     * 新增购物车
     */
    @Operation(summary = "新增购物车")
//    @PreAuthorize("@ss.hasPermi('system:cart:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProCart proCart) {
        proCart.setUserId(getUserId());
        proCart.setCreateBy(getUsername());
        return toAjax(proCartService.insertProCart(proCart));
    }

    /**
     * 修改购物车
     */
    @Operation(summary = "修改购物车")
//    @PreAuthorize("@ss.hasPermi('system:cart:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProCart proCart) {
        proCart.setUpdateBy(getUsername());
        return toAjax(proCartService.updateProCart(proCart));
    }

    /**
     * 删除购物车
     */
    @Operation(summary = "删除购物车")
//    @PreAuthorize("@ss.hasPermi('system:cart:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(proCartService.deleteProCartByIds(ids));
    }
}
